<?php namespace Admin\Models\User;
use \Laravel\Database\Eloquent\Model as Eloquent;
use Laravel\Auth, Laravel\Log, Admin\Models\User\Role;

class Acl extends Eloquent {

	public static $timestamps = true;
	public static $table = 'sys_user_acl';
	public static $key = 'sua_id';

	public static function regAcl($input){

		if(empty($input))
			return;

		foreach ($input['acl'] as $role => $access) {

			$thisRole = Role::find($role);

			$thisRole->acl()->delete();

			foreach ($access as $modul => $content) {
				
				foreach ($content as $controller => $action) {
					
					foreach ($action as $page => $exist) {
						
						if($exist){

						// $aclAccess = Acl::where('sur_id','=',$role)
						// 			->where('sua_modul','=',$modul)
						// 			->where('sua_controller','=',$controller)
						// 			->where('sua_action','=',$page)
						// 			->first();

						$aclAccess = new Acl;
						$aclAccess->created_at = date("Y-m-d H:i:s");
						$aclAccess->sur_id = $role;
						$aclAccess->sua_modul = $modul;
						$aclAccess->sua_controller = $controller;
						$aclAccess->sua_action = $page;
						$aclAccess->save();

						}

					}
				}
			}
		}

		Log::write('acl', 'ACL Registration Success By <code>'.Auth::user()->username.'</code>');
	}

	public static function allAcl(){

		$aclAll = Acl::all();

		$data = array();

		foreach ($aclAll as $acll) {
			$data[$acll->sur_id][$acll->sua_modul][$acll->sua_controller][$acll->sua_action] = 1;
		}

		return $data;
	}

}